///////////////////////////////////////////////////////////
//  IntersectionOperation.cpp
//  Implementation of the Class IntersectionOperation
//  Created on:      23-sep-2007 21:26:33
///////////////////////////////////////////////////////////

#include "IntersectionOperation.h"

#ifndef ZERO
	#define ZERO 0
#endif

IntersectionOperation::IntersectionOperation(){

}



IntersectionOperation::~IntersectionOperation(){

}


void IntersectionOperation::resolveOperation(ListPersistent& lsTwo, ListPersistent& lsOne){
	int idx1 = ZERO;
	int idx2 = ZERO;
	ListPersistent list;
	std::map<int,int> lsOk;
	list.setAtributte(lsTwo.getAtributte());
	std::string namePk = lsTwo.getPersistent(0)->getPrimaryKey()[0].nombre;
	

	for(idx1 = ZERO, idx2 = ZERO; idx2 < lsOne.getSize() && idx1 < lsTwo.getSize() ; )
	{
		Persistent* per1 = lsTwo.getPersistent(idx1);
		Persistent* per2 = lsOne.getPersistent(idx2);
		std::string val1 = per1->getValor(namePk);
		std::string val2 = per2->getValor(namePk);
		// revisar si necesito el isnumber
		if (val1 < val2){
			idx1++;
		}else{
			if (val2 < val1){
				idx2++;
			}else{
				std::pair<int,int> ins(idx1,idx1);
				lsOk.insert(ins);
				idx1++;
				idx2++;
			}
		}
	}
	
	for(int i = 0; i < lsTwo.getSize(); i++){
		if (lsOk.find(i)==lsOk.end()){
			lsTwo.erase(i);
		}
		
	}
}

